<template>
  <Dialog v-model="dialogVisible" :title="dialogTitle" width="1200px">
    <ContentWrap>
      <!-- 搜索工作栏 -->
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="姓名" prop="memberUserName">
          <el-input placeholder="请输入姓名" v-model.trim="queryParams.memberUserName" />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap>
      <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
        <el-table-column label="手续费负担" align="center" prop="payFeeStyle" width="100">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.ELEC_PAY_FEE_STYLE" :value="scope.row.payFeeStyle" />
          </template>
        </el-table-column>
        <el-table-column label="描述" align="left" prop="configRemark" min-width="130" />
        <el-table-column label="会员" align="center" prop="memberUserName" width="130" />
        <el-table-column label="最少支付" align="center" prop="minPayElecCharge" width="100">
          <template #default="scope">
            <div>{{ divide(scope.row.minPayElecCharge, 100) }}元</div>
          </template>
        </el-table-column>
        <el-table-column label="商户编号" align="center" prop="matCode" width="100" />
        <el-table-column label="余额提示" align="center" prop="warnElecMoney" width="100">
          <template #default="scope">
            <div>{{ divide(scope.row.warnElecMoney, 100) }}元</div>
          </template>
        </el-table-column>
        <el-table-column label="商户有效期" align="center" prop="matExpiresTime" width="100">
          <template #default="scope">
            <span>{{ formatDate(scope.row.matExpiresTime, 'YYYY-MM-DD') }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="120"
        >
          <template #default="scope">
            <el-button type="primary" link @click="confirmOwnMeterConfig(scope.row)"
              >选择</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </Dialog>
</template>
<script lang="ts" setup>
import { DICT_TYPE } from '@/utils/dict'
import { formatDate } from '@/utils/formatTime'
import { divide } from '@/utils/tools'
import { getOwnMeterConfigPage } from '@/api/zwsj/ownMeterConfig'

defineOptions({ name: 'OwnMeterConfigSelect' })

const emits = defineEmits(['confirmOwnMeterConfig'])

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref() // 列表的数据
const flag = ref('')

const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  memberUserName: ''
})

/** 打开弹窗 */
const open = async (flagT?) => {
  if(flagT != undefined && flagT != null){
    flag.value = flagT
  }
  dialogVisible.value = true
  dialogTitle.value = '选择银联账户'
  getList()
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const queryFormRef = ref() // 搜索的表单

/** 查询部门列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await getOwnMeterConfigPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  getList()
}

/** 确认 */
const confirmOwnMeterConfig = async (row: any) => {
  if(flag.value != undefined && flag.value != null && flag.value != ''){
    emits('confirmOwnMeterConfig', {matCode: row,flag: flag.value})
  }else{
    emits('confirmOwnMeterConfig', {matCode: row})
  }
  dialogVisible.value = false
}

/** 初始化 **/
onMounted(async () => {
  await getList()
})
</script>
